package com.renchunlin.looerpager.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.renchunlin.looerpager.R
import com.renchunlin.looerpager.bean.PagerItem
import com.renchunlin.looerpager.databinding.ActivitySuperMainBinding
import com.renchunlin.looerpager.view.SobLooperPager

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/6/17.
 * PS: Not easy to write code, please indicate.
 */
class SuperMainActivity : AppCompatActivity(),
    SobLooperPager.BindTitleListener {
    private lateinit var binding: ActivitySuperMainBinding

    /*private val newCarouselPagerAdapter by lazy {
        NewCarouselPagerAdapter(listPic)
    }*/

    private val listPic =
        listOf(
            PagerItem("第一个图片", R.drawable.pic1),
            PagerItem("第二个图片", R.drawable.pic2),
            PagerItem("第三个图片", R.drawable.pic3),
            PagerItem("第四个图片", R.drawable.pic4)
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySuperMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        val innerAdapter = object : SobLooperPager.InnerAdapter() {
            override fun getDataSize() = listPic.size

            override fun getSubView(container: ViewGroup, position: Int): View {
                val iv = ImageView(container.context)
                iv.setImageResource(listPic[position].picResId)
                iv.scaleType = ImageView.ScaleType.FIT_XY
                return iv
            }
        }

        binding.sobLooperPager.setData(innerAdapter, this)

        innerAdapter.setOnPagerItemClickListener(object :
            SobLooperPager.InnerAdapter.OnPagerItemClickListener {
            override fun onItemClick(position: Int) {
                Log.i("chen", "onItemClick: $position")
            }

        })
    }

    override fun getTitle(position: Int) = listPic[position].title
}